package eu.pago.chat.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import commonServer.CommonServer;

public class LoginFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	JPanel titlePanel;
	JPanel loginPanel;
	JTextField userTextField;
	JPasswordField passField;
	JButton loginButton;
	JPanel messagePanel;
	private Registry registry;

	public LoginFrame() {

		this.setLayout(new BorderLayout());

		try {

			try {
				registry = LocateRegistry.createRegistry(CommonServer.PORT);
			} catch (RemoteException exc) {
				registry = LocateRegistry.getRegistry(CommonServer.PORT);
			}
		} catch (RemoteException exc) {
			// TODO Auto-generated catch block
			exc.printStackTrace();
		}
		add(createLoginPanel(), BorderLayout.NORTH);
		add(createNewUserPanel(), BorderLayout.CENTER);
	}

	private JPanel createNewUserPanel() {
		JPanel newUserPanel = new JPanel();
		newUserPanel.setBackground(UtilColors.BLUE_NUANCE);
		// newUserPanel.setLayout(new GridLayout(2, 0));
		JLabel newUserLabel = new JLabel("     New user?     ");
		newUserLabel.setHorizontalAlignment(JLabel.CENTER);
		JButton newUserButton = new JButton("Sign up");
		ActionListener signUpListener = new InitSignUpListener(this, registry);
		newUserButton.addActionListener(signUpListener);

		newUserButton.setAlignmentX(CENTER_ALIGNMENT);
		newUserPanel.add(newUserLabel);
		newUserPanel.add(newUserButton);
		return newUserPanel;
	}

	private JPanel createLoginPanel() {
		loginPanel = new JPanel();
		loginPanel.setBackground(UtilColors.BLUE_NUANCE);
		loginPanel.setLayout(new GridLayout(5, 0));

		JPanel userPanel = new JPanel();
		userPanel.setBackground(UtilColors.BLUE_NUANCE);
		userPanel.setLayout(new GridLayout(0, 2));
		JLabel userLabel = new JLabel("Username: ");
		userTextField = new JTextField();
		userTextField.setBackground(UtilColors.FADE_YELLOW);
		userPanel.add(userLabel);
		userPanel.add(userTextField);

		JPanel passPanel = new JPanel();
		passPanel.setBackground(UtilColors.BLUE_NUANCE);
		passPanel.setLayout(new GridLayout(0, 2));
		JLabel passLabel = new JLabel("Password: ");
		passField = new JPasswordField();
		passField.setBackground(UtilColors.FADE_YELLOW);
		userPanel.add(passLabel);
		userPanel.add(passField);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(UtilColors.BLUE_NUANCE);
		loginButton = new JButton("Login");
		ActionListener clickButton = new ClickButton(this, registry);
		loginButton.addActionListener(clickButton);
		buttonPanel.add(loginButton);

		JLabel messageLabel = new JLabel("Login below:");
		messageLabel.setHorizontalAlignment(JLabel.CENTER);

		loginPanel.add(createTitlePanel());
		loginPanel.add(messageLabel);
		loginPanel.add(userPanel);
		loginPanel.add(passPanel);
		loginPanel.add(buttonPanel);
		return loginPanel;
	}

	private JPanel createTitlePanel() {
		titlePanel = new JPanel();
		titlePanel.setBackground(UtilColors.BLUE_NUANCE.brighter());
		titlePanel.setBorder(LineBorder.createBlackLineBorder());
		JLabel title1 = new JLabel("Pago");
		title1.setForeground(new Color(100, 0, 100));
		Font font1 = new Font("Serif", Font.ITALIC, 28);
		title1.setFont(font1);
		title1.setHorizontalTextPosition(JLabel.CENTER);
		title1.setVerticalTextPosition(JLabel.CENTER);
		titlePanel.add(title1);// ,BorderLayout.NORTH);
		JLabel title2 = new JLabel("Chat");
		title2.setForeground(new Color(200, 100, 0));
		title2.setFont(new Font("Serif", Font.ITALIC, 28));
		title2.setHorizontalTextPosition(JLabel.CENTER);
		title2.setVerticalTextPosition(JLabel.CENTER);
		titlePanel.add(title2);// ,BorderLayout.SOUTH);
		return titlePanel;
	}

	public JTextField getUserTextField() {
		return userTextField;
	}
}
